import serverApi from './sever.js'
import ksdCache from "./cache.js";
const request = (url = '', data = {}, type = 'GET', header = {}) => {
	//1：设置一个加载效果
	uni.showLoading();
	if (type === "POST") {
		//2：将参数序列化成JSON字符串
		header["Content-Type"] = "application/x-www-form-urlencoded";
	}
	//3：往请求头中放置token和用户id防止出现恶意获取数据 
	header.token = "www.feifei.com";
	//4: 获取服务器地址
	var base_url = serverApi.apiServer;
	// 4：发送异步请求
	return new Promise((resolve, reject) => {
		uni.request({
			method: type,
			url: base_url + url,
			data,
			header,
			dataType: 'json',
			success(res) {
				uni.hideLoading();
				if (res.data.code == 20000) {
					//获取响应数据
					resolve(res.data)
				} else {
					//如果失败了也要进行处理
					reject(res.data)
				}
			},
			fail(error) {
				let [err, res] = error;
				uni.hideLoading();
				uni.showToast({
					icon:"none",
					title:"网络出现故障!!!",
					duration:2000
				})
				reject(err)
			}
		});
	});
}
export default request
